package com.example.shop_2304.xq

import androidx.lifecycle.viewModelScope
import com.example.base.entity.AddCarEntity
import com.example.base.entity.Res
import com.example.base.entity.XqEntity
import com.example.base.mvvm.BaseViewModel
import com.example.base.utils.SingleLiveData
import kotlinx.coroutines.launch

class XqViewModel:BaseViewModel() {
    //商品详情
    val xqSuccess = SingleLiveData<Res<XqEntity>>()
    val xqFail = SingleLiveData<String>()
    fun xq(id:Int,type:String){
        viewModelScope.launch {
            try {
                xqSuccess.postValue(apiService.xq(id, type))
            } catch (e: Exception) {
               xqFail.postValue(e.message.toString())
            }
        }
    }

    //添加购物车
    val addSuccess = SingleLiveData<Res<AddCarEntity>>()
    val addFail = SingleLiveData<String>()
    fun addCar(cartNum:Int,productAttrUnique:String,productId:Int){
        viewModelScope.launch {
            try {
                val map = mutableMapOf<String,Any>()
                map["cartNum"] = cartNum
                map["productAttrUnique"] = productAttrUnique
                map["productId"] = productId
                addSuccess.postValue(apiService.addCar(mapToBody(map)))
            } catch (e: Exception) {
                addFail.postValue(e.message.toString())
            }
        }
    }
}